﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace assignment7
{
    public class OrderService
    {
        public List<Order> order = new List<Order>();

        public void addOrder(Order newOrder)
        {
            this.order.Add(newOrder);
        }

        public void removeOrder(int deleteId)
        {
            try
            {
                Console.WriteLine("请输入要删除的订单id：");
                //int deleteId = int.Parse(Console.ReadLine());
                int Index = 0;
                foreach (Order a in this.order)
                {
                    if (a.id == deleteId) Index = this.order.IndexOf(a);
                }
                this.order.RemoveAt(Index);
            }
            catch
            {
                Console.WriteLine("输入错误");
            }
        }

        public void refreshOrder(List<orderitem> orderitemlist)
        {
            try
            {
                Console.WriteLine("请输入你要修改的订单id：");
                int deleteId = int.Parse(Console.ReadLine());
                int Index = 0;
                foreach (Order a in this.order)
                {
                    if (a.id == deleteId) Index = this.order.IndexOf(a);
                }
                Console.WriteLine("该订单的customer是：");
                this.order[Index].customer = Console.ReadLine();
                Console.WriteLine("该订单的Date是：");
                this.order[Index].Date = Console.ReadLine();
                Console.WriteLine("该订单的orderitemlist是：");
                this.order[Index].orderitems = orderitemlist;
                //Console.WriteLine("该订单的Money是：");
                this.order[Index].Money = this.order[Index].getPriceInAll();
                this.order[Index].showorderItem();
            }
            catch
            {
                Console.WriteLine("输入错误");
            }
        }

        public void searchOrder(int i)
        {
            try
            {
                switch (i)
                {
                    case 1: //按金额进行查询
                        int maxnum; int minnum;
                        Console.WriteLine("请输入要查询的最小金额");
                        minnum = Convert.ToInt32(Console.ReadLine());
                        Console.WriteLine("请输入要查询的最大金额");
                        maxnum = Convert.ToInt32(Console.ReadLine());

                        var query1 = from s1 in order
                                     where maxnum > s1.Money
                                     orderby s1.Money
                                     select s1;
                        var query2 = from s2 in query1
                                     where minnum < s2.Money
                                     orderby s2.Money
                                     select s2;
                        List<Order> a1 = query2.ToList();
                        Console.WriteLine("订单号   顾客    日期    订单价格");
                        foreach (Order b1 in a1)
                        {
                            Console.WriteLine("-----------------------------------");
                            Console.WriteLine("{0} {1} {2} {3}", b1.id, b1.customer, b1.Date, b1.Money);
                        }
                        break;

                    case 2://按客户名进行查询
                        Console.WriteLine("请输入客户名称：");
                        string name = Console.ReadLine();
                        var query3 = from s3 in this.order
                                     where s3.customer == name
                                     orderby s3.Money
                                     select s3;
                        List<Order> a2 = query3.ToList();
                        Console.WriteLine("订单号   顾客    日期    订单价格");
                        foreach (Order b2 in a2)
                        {
                            Console.WriteLine("-----------------------------------");
                            Console.WriteLine("{0} {1} {2} {3}", b2.id, b2.customer, b2.Date, b2.Money);
                        }
                        break;
                    case 3:
                        Console.WriteLine("请输入订单编号");
                        int id = int.Parse(Console.ReadLine());
                        var query4 = from s4 in this.order
                                     where s4.id == id
                                     orderby s4.Money
                                     select s4;
                        List<Order> a3 = query4.ToList();
                        Console.WriteLine("订单号   顾客   日期   订单价格");
                        foreach (Order b3 in a3)
                        {
                            Console.WriteLine("-------------------------------------------");
                            Console.WriteLine("{0} {1} {2} {3}", b3.id, b3.customer, b3.Date, b3.Money);
                        }
                        break;

                    case 4:
                        Console.WriteLine("请输入商品名称：");
                        string itemName = Console.ReadLine();
                        var query5 = from s5 in this.order
                                     where s5.orderitems.Exists(
                                        x => x.Name == itemName
                                     )
                                     orderby s5.Money
                                     select s5;
                        List<Order> a4 = query5.ToList();
                        Console.WriteLine("订单号   顾客   日期   订单价格");
                        foreach (Order b4 in a4)
                        {
                            Console.WriteLine("--------------------------------------------");
                            Console.WriteLine("{0} {1} {2} {3}", b4.id, b4.customer, b4.Date, b4.Money);
                        }
                        break;
                    default:
                        Console.WriteLine("您输入的选项不存在");
                        break;
                }
            }
            catch
            {
                Console.WriteLine("输入错误");
            }

        }
    }
}
